package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityMjQrList;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.utils.DateUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoMjQrList;
import com.ocom.srvpaccess.service.MjQrListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MjQrListServiceImpl extends ServiceImpl<IDaoMjQrList, EntityMjQrList> implements MjQrListService {

    @Autowired
    private IDaoMjQrList daoMjQrList;

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityMjQrList selectQrInfo(Long companyId, String machNo, String qrCode) {
        QueryWrapper<EntityMjQrList> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("device_no",machNo);
        query.eq("qr_code",qrCode);
        query.gt("end_time",DateUtils.getTime());
        query.last("limit 1");
        return daoMjQrList.selectOne(query);
    }
    //修改总库mj_qr_list表：
    //	update mj_qr_list
    //set result_code =1, result_msg = “设备验证成功”
    //				where qr_code = qrCode
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public String updateResult(Long companyId, String machNo, String qrCode) {
        QueryWrapper<EntityMjQrList> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("device_no",machNo);
        query.eq("qr_code",qrCode);
        EntityMjQrList qrList = new EntityMjQrList();
        qrList.setResultCode(1);
        qrList.setResultMsg("设备验证成功");
        daoMjQrList.update(qrList,query);
        return qrList.getResultMsg();
    }
}
